package com.am.mosl.cityandroid.widget;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;

import com.am.mosl.cityandroid.R;
import com.am.mosl.cityandroid.leftmenu.SlidMenuAdapter;
import com.am.mosl.cityandroid.message.LoginMessage;
import com.am.mosl.cityandroid.message.MessageNumber;
import com.am.mosl.cityandroid.leftmenu.AboutActivity;
import com.am.mosl.cityandroid.leftmenu.ActivityedActivity;
import com.am.mosl.cityandroid.leftmenu.FeedBackActivity;
import com.am.mosl.cityandroid.leftmenu.MessageCentetActivity;
import com.am.mosl.cityandroid.login.LoginActivity;
import com.am.mosl.cityandroid.login.NewAccountActivity;
import com.am.mosl.cityandroid.util.DialogUtil;
import com.am.mosl.cityandroid.util.SettingUtils;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import de.greenrobot.event.EventBus;

/**
 *
 * */
public class DrawerView implements OnClickListener {

	private final Activity activity;
	SlidingMenu localSlidingMenu;
	private ListView lvMenu;
	private String[] lvMenuItems;
	private SlidMenuAdapter mSlideMenuAdapter;

	public DrawerView(Activity activity) {
		this.activity = activity;
	}

	public SlidingMenu initSlidingMenu() {
		localSlidingMenu = new SlidingMenu(activity);
		localSlidingMenu.setMode(SlidingMenu.LEFT); // 左边滑动
		localSlidingMenu.setTouchModeAbove(SlidingMenu.SLIDING_WINDOW);
		localSlidingMenu.setShadowWidthRes(R.dimen.shadow_width);// 阴影
		localSlidingMenu.setShadowDrawable(R.drawable.shadow);//
		localSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);//
		localSlidingMenu.setFadeDegree(0.35F);
		localSlidingMenu.attachToActivity(activity, SlidingMenu.RIGHT);
		localSlidingMenu.setMenu(R.layout.left_drawer_fragment);
		localSlidingMenu
				.setOnOpenedListener(new SlidingMenu.OnOpenedListener() {
					public void onOpened() {

					}
				});
		initView();
        if(SettingUtils.IsLogin(activity)){
            lvMenu.setFooterDividersEnabled(true);
        }else {
            lvMenu.setFooterDividersEnabled(false);
        }
		return localSlidingMenu;
	}

	public void updateSlideMenu() {
		mSlideMenuAdapter.notifyDataSetChanged();
        if(SettingUtils.IsLogin(activity)){
            lvMenu.setFooterDividersEnabled(true);
        }else {
            lvMenu.setFooterDividersEnabled(false);
        }
        }

    public void setMessageNumebr(int number){
        mSlideMenuAdapter.setMessageNumber(number);
        if(SettingUtils.IsLogin(activity)){
            lvMenu.setFooterDividersEnabled(true);
        }else{
            lvMenu.setFooterDividersEnabled(false);
        }
        mSlideMenuAdapter.notifyDataSetChanged();
    }

	public void initView() {

		lvMenuItems = activity.getResources()
				.getStringArray(R.array.menu_items);
		lvMenu = (ListView) localSlidingMenu
				.findViewById(R.id.activity_main_menu_listview);

		List<Map<String, String>> listMapMenu = new ArrayList<Map<String, String>>();
		String[] menuString = activity.getResources().getStringArray(
				R.array.menu_items);
		Integer[] imageInteger = new Integer[] { R.drawable.sidebar_activity,
				R.drawable.sidebar_msg, R.drawable.sidebar_user,
				R.drawable.sidebar_feedback, R.drawable.sidebar_about,
				R.drawable.sidebar_exit };
		for (int i = 0; i < menuString.length; i++) {
			HashMap<String, String> mapMenu = new HashMap<String, String>();
			mapMenu.put("0", imageInteger[i].toString());
			mapMenu.put("1", menuString[i]);
			listMapMenu.add(mapMenu);
		}
		mSlideMenuAdapter = new SlidMenuAdapter(activity, listMapMenu);
		lvMenu.setAdapter(mSlideMenuAdapter);
		lvMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				onMenuItemClick(parent, view, position, id);
			}

		});
	}

	private void onMenuItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		switch (position) {
		case 0: // 点击头像
			if (!SettingUtils.IsLogin(activity))
				activity.startActivity(new Intent(activity, LoginActivity.class));
			break;
		case 1: // 已报名活动
			activity.startActivity(new Intent(activity,
					ActivityedActivity.class));
			break;
		case 2: // 消息中心
			if (SettingUtils.IsLogin(activity)) {
				
				 LoginMessage message=new LoginMessage();
                 message.setType(1);
                 EventBus.getDefault().post(message);
                 
                 
                 MessageNumber message2 = new MessageNumber();
                 message2.setStatus(MessageNumber.Status_Cancel);
                 EventBus.getDefault().post(message2);
                 
             
				activity.startActivity(new Intent(activity,
						MessageCentetActivity.class));
			} else {
				
				 LoginMessage message=new LoginMessage();
                 message.setType(0);
                 EventBus.getDefault().post(message);
                 
				activity.startActivity(new Intent(activity, LoginActivity.class));
			}
			break;
		case 3: // 个人资料

			if (SettingUtils.IsLogin(activity)) {
				Intent intent = new Intent(activity, NewAccountActivity.class);
				intent.putExtra("Personal", "1");
				intent.putExtra("Phone",
						SettingUtils.getUserInfo(activity).data.getPhone());
				intent.putExtra("Verify", "0");
				activity.startActivity(intent);
			} else {
				activity.startActivity(new Intent(activity, LoginActivity.class));
			}

			break;
		case 4: // 给我留言
			activity.startActivity(new Intent(activity, FeedBackActivity.class));
			break;
		case 5: // 关于
			activity.startActivity(new Intent(activity, AboutActivity.class));
			break;
		default:
			DialogUtil.QuitDialog(activity);
		}
		localSlidingMenu.showContent(); // 隐藏菜单
	}


	@Override
	public void onClick(View v) {

	}
}
